const BaseService = require('./base_service.js');
const FosterRoomModel = require('../model/foster_room_model.js');

class FosterRoomService extends BaseService {

  /**
   * 添加寄养房间
   */
  async addRoom(input) {
    const db = uniCloud.database();
    const collection = db.collection(FosterRoomModel.CL);

    // 检查房间编号是否重复
    const existingRoom = await collection.where({
      roomId: input.roomId,
      storeId: input.storeId
    }).get();

    if (existingRoom.data.length > 0) {
      throw new Error('房间编号已存在');
    }

    // 创建房间数据
    const roomData = {
      roomId: input.roomId,
      roomName: input.roomName,
      roomType: input.roomType,
      size: input.size || '',
      capacity: input.capacity,
      price: input.price,
      unit: input.unit || '只/天/不含粮',
      description: input.description || '',
      images: input.images || [],
      amenities: input.amenities || [],
      careInstructions: input.careInstructions || '',
      storeId: input.storeId,
      status: 1, // 默认可用
      isOccupied: false,
      currentPets: [],
      createTime: Date.now(),
      updateTime: Date.now()
    };

    const result = await collection.add(roomData);
    
    return {
      roomId: result.id,
      message: '房间添加成功'
    };
  }

  /**
   * 删除寄养房间
   */
  async deleteRoom(roomId) {
    const db = uniCloud.database();
    const collection = db.collection(FosterRoomModel.CL);

    // 检查房间是否存在
    const room = await collection.doc(roomId).get();
    if (!room.data.length) {
      throw new Error('房间不存在');
    }

    // 检查房间是否被占用
    if (room.data[0].isOccupied) {
      throw new Error('房间正在使用中，无法删除');
    }

    // 软删除
    await collection.doc(roomId).update({
      status: 0,
      updateTime: Date.now()
    });

    return { message: '房间删除成功' };
  }

  /**
   * 更新寄养房间
   */
  async updateRoom(roomId, input) {
    const db = uniCloud.database();
    const collection = db.collection(FosterRoomModel.CL);

    // 检查房间是否存在
    const room = await collection.doc(roomId).get();
    if (!room.data.length) {
      throw new Error('房间不存在');
    }

    // 更新房间数据
    const updateData = {
      ...input,
      updateTime: Date.now()
    };

    await collection.doc(roomId).update(updateData);

    return { message: '房间更新成功' };
  }

  /**
   * 获取房间列表
   */
  async getRoomList(input) {
    const db = uniCloud.database();
    const collection = db.collection(FosterRoomModel.CL);

    // 构建查询条件
    let whereCondition = {};
    
    if (input.storeId) {
      whereCondition.storeId = input.storeId;
    }
    
    if (input.roomType) {
      whereCondition.roomType = input.roomType;
    }
    
    if (input.status !== undefined) {
      whereCondition.status = input.status;
    }
    
    if (input.search) {
      whereCondition.roomName = new RegExp(input.search, 'i');
    }

    // 分页查询
    const result = await collection
      .where(whereCondition)
      .orderBy('createTime', 'desc')
      .skip((input.page - 1) * input.size)
      .limit(input.size)
      .get();

    // 获取总数
    const countResult = await collection.where(whereCondition).count();

    return {
      list: result.data,
      total: countResult.total,
      page: input.page,
      size: input.size
    };
  }
}

module.exports = FosterRoomService;
